<?php  
class ControllerModulePagenav extends Controller {
	
	protected function index($setting) {
		$this->language->load('module/pagenav');
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
		
    	if(isset($this->request->get['route'])) {
    		$route = $this->request->get['route'];
    		$this->data['route'] = $route;
    	}else{
    		$this->data['route'] = '';
    	}
    	
		if (isset($this->request->get['path'])) {
			$parts = explode('_', (string)$this->request->get['path']);
		} else {
			$parts = array();
		}
		
		if (isset($parts[0])) {
			$this->data['page_id'] = $parts[0];
		} else {
			$this->data['page_id'] = 0;
		}
		
		if (isset($parts[1])) {
			$this->data['child_id'] = $parts[1];
		} else {
			$this->data['child_id'] = 0;
		}
							
		$this->load->model('page/page');
		
		$this->data['pages'] = array();
					
		$pages = $this->model_page_page->getPagesByParentId(0);
		
		foreach ($pages as $page) {
			$children_data = array();
			$children_route = array();
			
			$children = $this->model_page_page->getPagesByParentId($page['page_id']);
			
			foreach ($children as $child) {
				
				$url = $this->url->link('page/page', 'path=' . $page['page_id'] . '_' . $child['page_id']);
				
				if($child['type'] == 'route') {
					$url = $this->url->link($child['content'],'','SSL');
					$route = $child['content'];
					$children_route[] = $route;
				} else {
					$route = '';
				}
				
				$children_data[] = array(
					'page_id' => $child['page_id'],
					'name'        => $child['title'],
					'href'        => $url,
					'route'		  => $route
				);						
			}
			
			$url = $this->url->link('page/page', 'path=' . $page['page_id']);
			
			if($page['type'] == 'route') {
				$url = $this->url->link($page['content'],'','SSL');
				$route = $page['content'];
			} else {
				$route = '';
			}
			
			$this->data['pages'][] = array(
				'page_id' => $page['page_id'],
				'name'        => $page['title'],
				'children'    => $children_data,
				'href'        => $url,
				'children_route'		  => $children_route
			);			
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/pagenav.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/pagenav.tpl';
		} else {
			$this->template = 'default/template/module/pagenav.tpl';
		}
		
		$this->render();
  	}
  	
}
?>